package com.enqualcomm.kids.config.userdefault;

import com.enqualcomm.kids.component.MyApplication;
import com.enqualcomm.kids.network.http.response.GetUserInfoResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import common.utils.BufferedSharedPreferences;
import common.utils.GsonFactory;

/**
 * Created by yinzhiqun on 2015/10/14.
 *
 */
public class UserDefault {

    private final BufferedSharedPreferences sp;
    private final String userid;

    public UserDefault(String userid) {
        this.userid = userid;
        sp = MyApplication.getInstance().getSp(userid+"_u");
    }

    public String getUserid(){
        return userid;
    }

    public void setTerminals(List<TerminallistResult.Terminal> terminals){
        CacheEntry.set(Keys.User.TERMINAL_LIST+userid,terminals);

        if (terminals!=null && terminals.size()!=0){
            String s = GsonFactory.newInstance().toJson(terminals);
            sp.saveString(Keys.User.TERMINAL_LIST,s);
        }else{
            sp.remove(Keys.User.TERMINAL_LIST);
        }
    }

    public List<TerminallistResult.Terminal> getTerminals(){
        List<TerminallistResult.Terminal> list = (List<TerminallistResult.Terminal>) CacheEntry.get(Keys.User.TERMINAL_LIST+userid);

        if (list!=null){
            return list;
        }else {
            String s = sp.getString(Keys.User.TERMINAL_LIST, null);
            if (s!=null){
                list = GsonFactory.newInstance().fromJson(s, new TypeToken<List<TerminallistResult.Terminal>>() {
                }.getType());
                CacheEntry.set(Keys.User.TERMINAL_LIST+userid,list);
                return  list;
            }
            return null;
        }
    }

    public void setMapUISettings(String settings){
        CacheEntry.set(Keys.User.MAP_UISETTINGS+userid,settings);

        if (settings!=null){
            sp.saveString(Keys.User.MAP_UISETTINGS,settings);
        }else{
            sp.remove(Keys.User.MAP_UISETTINGS);
        }
    }

    public String getMapUISettings(){
        String settings = (String) CacheEntry.get(Keys.User.MAP_UISETTINGS+userid);
        if (settings==null){
            settings = sp.getString(Keys.User.MAP_UISETTINGS, null);
            CacheEntry.set(Keys.User.MAP_UISETTINGS + userid, settings);
        }
        return settings;
    }
//
//    public void setNewUserConfig(String settings){
//        CacheEntry.set(Keys.User.NEWUSERCONFIG+userid,settings);
//
//        if (settings!=null){
//            sp.saveString(Keys.User.NEWUSERCONFIG,settings);
//        }else{
//            sp.remove(Keys.User.NEWUSERCONFIG);
//        }
//    }
//    public String getNewUserConfig(){
//        String settings = (String) CacheEntry.get(Keys.User.NEWUSERCONFIG+userid);
//        if (settings==null){
//            settings = sp.getString(Keys.User.NEWUSERCONFIG, "0");
//            CacheEntry.set(Keys.User.MAP_UISETTINGS + userid, settings);
//        }
//        return settings;
//    }




    public String getHeader(){
        GetUserInfoResult info = getInfo();
        if (info!=null){
            return info.pictureid;
        }
        return null;
    }

    public void setInfo(GetUserInfoResult info){
        CacheEntry.set(Keys.User.INFO+userid,info);

        if (info!=null){
            String s = GsonFactory.newInstance().toJson(info);
            sp.saveString(Keys.User.INFO,s);
        }else{
            sp.remove(Keys.User.INFO);
        }
    }

    public GetUserInfoResult getInfo(){
        GetUserInfoResult info = (GetUserInfoResult) CacheEntry.get(Keys.User.INFO+userid);

        if (info!=null){
            return info;
        }else {
            String s = sp.getString(Keys.User.INFO, null);
            if (s!=null){
                info = GsonFactory.newInstance().fromJson(s, GetUserInfoResult.class);
                CacheEntry.set(Keys.User.INFO+userid,info);
                return  info;
            }
            return null;
        }
    }

    public boolean isOpenGestureLock(){
        return sp.getBoolean("isOpenGestureLock",false);
    }

    public void setStartOpenGestureLock(boolean isOpenGestureLock){
        sp.saveBoolean("isOpenGestureLock",isOpenGestureLock);
    }

    public void saveGestureLock(String gestureLock){
        sp.saveString("gesture_key",gestureLock);
    }

    public String getGestureKey(){
        return sp.getString("gesture_key","");
    }

    public boolean isSetGestureLock(){
        return sp.getBoolean("isSetGesture",false);
    }

    public void setGestureLock(){
        sp.saveBoolean("isSetGesture",true);
    }

    public void setLoginOtherResult(LoginOtherResult result){
        CacheEntry.set(Keys.User.OTHER_LOGIN_INFO + userid, result);

        if (result != null) {
            String s = GsonFactory.newInstance().toJson(result);
            sp.saveString(Keys.User.OTHER_LOGIN_INFO, s);
        } else {
            sp.remove(Keys.User.OTHER_LOGIN_INFO);
        }
    }
    public LoginOtherResult getLoginOtherResult(){
        LoginOtherResult info = (LoginOtherResult) CacheEntry.get(Keys.User.OTHER_LOGIN_INFO + userid);

        if (info != null) {
            return info;
        } else {
            String s = sp.getString(Keys.User.OTHER_LOGIN_INFO, null);
            if (s != null) {
                info = GsonFactory.newInstance().fromJson(s, LoginOtherResult.class);
                CacheEntry.set(Keys.User.OTHER_LOGIN_INFO + userid, info);
                return info;
            }
            return null;
        }
    }
}
